package com.example.cmpe277stockdata;

import java.util.ArrayList;
import java.util.List;

import com.example.cmpe277stockdata.beans.NewsBean;
import com.example.cmpe277stockdata.beans.UserBean;
import com.example.cmpe277stockdata.constants.DataConstants;

import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;

public class NewsActivity extends Activity {

	private Context ctx;
	private List<NewsBean> result = null;
	private ArrayList<NewsBean> newsList = null;
	private String companyName,tickerSymbol;
	private TextView headerView;
	private ListView listviewNews;
	private UserBean user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_activity);
		
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
		    getActionBar().setDisplayHomeAsUpEnabled(true);
		}
		
		ctx=this;
		//Displaying the news details obtained from the RSS Reader
		result = (List<NewsBean>) getIntent().getSerializableExtra(DataConstants.NEWS_RESULT);
		newsList = (ArrayList<NewsBean>) getIntent().getSerializableExtra(DataConstants.NEWS_RESULT);
		companyName = getIntent().getStringExtra(DataConstants.COMPANY_NAME);
		tickerSymbol = getIntent().getStringExtra(DataConstants.TICKER_SYMBOL);
		listviewNews = (ListView) findViewById(R.id.lstCompanyNews);
		user = (UserBean) getIntent().getSerializableExtra(DataConstants.USER_DATA);
		headerView = (TextView) findViewById(R.id.txtCompanyName);
		headerView.setText(companyName);
		
		listviewNews.setAdapter( new NewsAdapter(ctx, R.layout.news_data, result));
			
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.news, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch(item.getItemId()) {
	    case R.id.action_home:
	    	if(user != null)
	    	{
	    		Intent intent = new Intent(this, InitialTabContent.class);
				intent.putExtra(DataConstants.USER_DATA, user);
		        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		        startActivity(intent);
	    	}
	    	else
	    	{
	    		Intent intent = new Intent(this, LandingActivity.class);
				intent.putExtra(DataConstants.USER_DATA, user);
		        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		        startActivity(intent);
	    	}
	        return true;
	    case R.id.action_historical:
	        Intent intent1 = new Intent(this, HistoricalActivity.class);
	        intent1.putExtra(DataConstants.NEWS_RESULT, newsList);
        	intent1.putExtra(DataConstants.COMPANY_NAME, companyName);
        	intent1.putExtra(DataConstants.TICKER_SYMBOL,tickerSymbol);
	        intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			intent1.putExtra(DataConstants.USER_DATA, user);
	        startActivity(intent1);
	        return true;
	    case R.id.action_companyinfo:
	        Intent intent2 = new Intent(this, CompanyInformationActivity.class);
	        intent2.putExtra(DataConstants.NEWS_RESULT, newsList);
        	intent2.putExtra(DataConstants.COMPANY_NAME, companyName);
        	intent2.putExtra(DataConstants.TICKER_SYMBOL,tickerSymbol);
			intent2.putExtra(DataConstants.USER_DATA, user);
	        intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        startActivity(intent2);
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
}
	